from django.shortcuts import render
from django.views import View
from apps.contents.models import GoodsCategory
from apps.goods.models import SKU
from django import http
from django.core.paginator import Paginator, EmptyPage
from apps.goods.utils import get_breadcrumb
from haystack.views import SearchView


# Create your views here.

class ListView(View):
    def get(self, request, category_id):
        page = request.GET.get('page')
        page_size = request.GET.get('page_size')
        ordering = request.GET.get('ordering')

        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExsit:
            return http.JsonResponse({'code': 400, 'errmsg': '参数category_id不存在'})

        skus = SKU.objects.filter(category=category, is_launched=True).order_by(ordering)

        paginator = Paginator(skus, page_size)
        try:
            page_skus = paginator.page(page)
        except EmptyPage:
            return http.JsonResponse({'code': 400, 'errmsg': '获取当前页数据错误'})

        count = paginator.num_pages

        list = []
        for sku in page_skus:
            list.append({
                "id": sku.id,
                "default_image_url": sku.default_image.url,
                "name": sku.name,
                "price": sku.price
            })

        breadcrumb = get_breadcrumb(category)

        return http.JsonResponse({
            "code": 0,
            "errmsg": "ok",
            "breadcrumb": breadcrumb,  # 面包屑数据
            "list": list,
            "count": count  # 分页总数
        })


class HotGoodsView(View):
    def get(self, request, category_id):
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExsit:
            return http.JsonResponse({'code': 400, 'errmsg': '参数category_id不存在'})

        skus = SKU.objects.filter(category=category, is_launched=True).order_by('-sales')[:2]

        hot_skus_list = []
        for sku in skus:
            hot_skus_list.append({
                "id": sku.id,
                "default_image_url": sku.default_image.url,
                "name": sku.name,
                "price": sku.price
            })

        return http.JsonResponse({
            "code": "0",
            "errmsg": "OK",
            "hot_skus": hot_skus_list
        })


class MySearchView(SearchView):
    '''重写SearchView类'''
    def create_response(self):
        context = self.get_context()
        data_list = []
        for result in context['page'].object_list:
            data_list.append({
                'id': result.object.id,
                'name': result.object.name,
                'price': result.object.price,
                'default_image_url': result.object.default_image.url,
                'searchkey': context.get('query'),
                'page_size': context['page'].paginator.num_pages,
                'count': context['page'].paginator.count
            })

        # data_list是列表 防止被序列化，安全参数设置false
        return http.JsonResponse(data_list,safe=False)
